# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userOaurhAppDao import UserOauthAppDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
"""
表名：zzh_user_oauth
说明：OAuth授权用户信息表
"""


CORS(app, supports_credentials=True)

@app.route('/queryUserOauthAppById', methods=['POST'])
def queryUserOauthAppByIdNoToken():

    if not request.json:
        abort(400)
    id = request.json.get("id")

    try:
        userOauthAppJSON, total_count= UserOauthAppDao().queryUserOauthAppById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserOauthApp OK"},"info":userOauthAppJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserOauthAppByCondition', methods=['POST'])
def queryUserOauthAppByConditionNoToken():

    if not request.json:
        abort(400)
    appId = request.json.get("appId")
    pageIndex = request.json.get("pageIndex",1)#传递页面索引号，默认为1
    pageSize = request.json.get("pageSize",10)

    try:
        total_count, rs = UserOauthAppDao().queryUserOauthAppByCondition(appId=appId,pageIndex=pageIndex,pageSize=pageSize)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserOauthApp OK"}, "totalCount": total_count, "info": rs})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertUserOauthApp', methods=['POST'])
def insertUserOauthAppNoToken():

    if not request.json:
        abort(400)

    Info = {
        'appTitle': request.json.get('appTitle'),  # 必填，请传递userd
        'appImgUrl': request.json.get('appImgUrl'),  #
        'appId': request.json.get('appId'),  #
        'appKey': request.json.get('appKey'),  #
        'remark': request.json.get('remark'),#
        'appSort': request.json.get('appSort'),  #
        'appIsLock': request.json.get('appIsLock'),  #
        'apiPath': request.json.get('apiPath')
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserOauthAppDao().addUserOauthApp(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserOauthApp OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "updateUserOauthApp Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})


@app.route('/updateUserOauthApp', methods=['POST'])
def updateUserOauthAppNoToken():

    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = UserOauthAppDao().selectOauthApp(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
        'id':request.json['id'],
        'appTitle': request.json.get('appTitle',oldInfo[1]),  # 必填，请传递userd
        'appImgUrl': request.json.get('appImgUrl',oldInfo[2]),  #
        'appId': request.json.get('appId',oldInfo[3]),  #
        'appKey': request.json.get('appKey',oldInfo[4]),  #
        'remark': request.json.get('remark',oldInfo[5]),#
        'appSort': request.json.get('appSort',oldInfo[6]),  #
        'appIsLock': request.json.get('appIsLock',oldInfo[7]),  #
        'apiPath': request.json.get('apiPath',oldInfo[8])
    }




    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserOauthAppDao().updateUserOauthApp(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateUserOauthApp OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateUserOauthApp Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteUserOauthApp', methods=['POST'])  # 根据id数组删除zzh_user_recharge表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserOauthAppNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的userRecharge id数组！"})
    try:
        rs = UserOauthAppDao().deleteUserOauthApp(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUserOauthApp OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUserOauthApp Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


